package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.UserDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /*实现条件查询或则查询全部*/
    @Override
    public Result userPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Result result=null;
        if(queryPageBean.getQueryString()==null){
            List<User> list= userDao.findAll();
            //对返回的list进行封装
            PageInfo<User> pageInfo=new PageInfo<>(list);
            PageResult pageResult = new PageResult(pageInfo.getTotal(), list);
            result= new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        }else{
            List<User> list= userDao.findById(queryPageBean.getQueryString());
            //对返回的list进行封装
            PageInfo<User> pageInfo=new PageInfo<>(list);
            PageResult pageResult = new PageResult(pageInfo.getTotal(), list);
            result= new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
      }
        return  result;
    }

    /**
     *
     * @param idStr
     * @param user
     */
    @Override
    public void user(String idStr, User user) {
        user.setPassword(MD5Utils.md5(user.getPassword()));
        System.out.println("===============================================================");
        System.out.println("===============================================================");
        System.out.println("===============================================================");
        System.out.println("===============================================================");
        System.out.println("===============================================================");
        System.out.println(user.getPassword());
        userDao.user(user);
        Integer userId = user.getId();
        System.out.println(userId);
        try {
            String[] idStrArr = idStr.split(",");
            for (String roleId1 : idStrArr) {
                if(!StringUtils.isEmpty(roleId1)){
                    Integer roleId = Integer.valueOf(roleId1);
                    userDao.roleIds(userId,roleId);
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Result selectUser(Integer id) {
        User user = userDao.selectUser(id);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,user);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Result selectUserRole(Integer id) {
        List<Role> list = userDao.selectUserRole(id);
        List<Integer> roleIds = new ArrayList<>();
        for (Role role : list) {
            roleIds.add(role.getId());
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,roleIds);
    }

    /**
     *
     * @param idStr
     * @param user
     */
    @Override
    public void user1(String idStr, User user) {
        System.out.println(idStr);//1,3,2
        userDao.user1(user);
        Integer userId = user.getId();//拿到userId
        userDao.deletByUserID(userId);
        String[] idStrArr = idStr.split(",");
        for (String roleId1 : idStrArr) {
            Integer roleId = Integer.parseInt(roleId1);
            userDao.roleIds1(userId,roleId);
        }
//        for (String roleId1 : idStrArr) {
//            Integer roleId = Integer.valueOf(roleId1);
//            userDao.roleIds1(userId,roleId);
//        }
    }
    /*删除角色信息还有角色权限*/
    @Override
    public void deletByUserId(Integer id) {
        userDao.deletUserRoleByUserId(id);
        userDao.deletUserByUserId(id);
    }

    //根据用户名查询菜单列表
    @Override
    public List<Menu> findMuneByUserName(String username) {

        //通过username查询到对应的角色
        List<Menu> list = userDao.findMuneByUserName(username);

        //得到所有的菜单id
        List<Integer> ids = list.stream().map(Menu::getId).collect(Collectors.toList());

//        通过所有的菜单id查询子菜单
        for (Integer id : ids) {
                List<Menu> list2 = userDao.findParentMenuById(id);
                //给子菜单菜单赋值
                list.get(id).setChildren(list2);
        }
        return list;
    }


}
